<template>
  <j-modal
    :title="title"
    :width="1200"
    :visible="visible"
    :maskClosable="false"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    :footer="false"
    @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">
      <!-- 主表单区域 -->
      <j-form-container :disabled="formDisabled">
      <a-form :form="form"  >
        <a-row>
          <a-col :xs="24" :sm="12">
            <a-form-item label="优惠券编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['couponId']" placeholder="请输入优惠券编号"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="客户编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['customerId', validatorRules.customerId]" placeholder="请输入客户编号"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="所属门店" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['shopName']" placeholder="请输入所属门店"></a-input>
            </a-form-item>

          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="订单总金额" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['totalAmount', validatorRules.totalAmount]" placeholder="请输入订单总金额" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="应付金额" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['payAmount', validatorRules.payAmount]" placeholder="请输入应付金额（实际支付金额）" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="运费金额" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['freightAmount', validatorRules.freightAmount]" placeholder="请输入运费金额" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="优惠券抵扣金额" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['couponAmount', validatorRules.couponAmount]" placeholder="请输入优惠券抵扣金额" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="支付方式" :labelCol="labelCol" :wrapperCol="wrapperCol">
<!--              <a-input-number v-decorator="['payType', validatorRules.payType]" placeholder="请输入支付方式[0:未支付,1:微信,2:支付宝]" style="width: 100%"/>-->
              <j-dict-select-tag type="list" v-decorator="['payType', validatorRules.payType]" :trigger-change="true" dictCode="biz_order_pay_type" placeholder="请输入支付方式"/>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="支付编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['payNo']" placeholder="请输入支付编号"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="退款编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['refundNo']" placeholder="请输入退款编号[微信退款需要]"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="订单来源" :labelCol="labelCol" :wrapperCol="wrapperCol">
<!--              <a-input-number v-decorator="['sourceType', validatorRules.sourceType]" placeholder="请输入订单来源[1:微信小程序,2:app,3:pc]" style="width: 100%"/>-->
              <j-dict-select-tag type="list" v-decorator="['sourceType']" :trigger-change="true" dictCode="biz_order_source" placeholder="订单来源"/>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="订单状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['status']" :trigger-change="true" dictCode="biz_order_status" placeholder="请选择订单状态"/>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="订单类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
<!--              <a-input-number v-decorator="['orderType', validatorRules.orderType]" placeholder="请输入订单类型[0:正常订单,1:团购订单]" style="width: 100%"/>-->
              <j-dict-select-tag type="list" v-decorator="['orderType']" :trigger-change="true" dictCode="biz_order_type" placeholder="订单类型"/>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="物流公司(配送方式)" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['deliveryCompany']" placeholder="请输入物流公司(配送方式)"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="物流单号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['deliveryCode']" placeholder="请输入物流单号"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="自动确认时间（天）" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['autoConfirmDay']" placeholder="请输入自动确认时间（天）" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="可以获得的积分" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['integral', validatorRules.integral]" placeholder="请输入可以获得的积分" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <!-- <a-col :xs="24" :sm="12">
            <a-form-item label="收货人" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['receiverName']" placeholder="请输入收货人"></a-input>
            </a-form-item>
          </a-col> -->
          <a-col :xs="24" :sm="12">
            <a-form-item label="收货人电话" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['receiverPhone']" placeholder="请输入收货人电话"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="收货人邮编" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['receiverPostCode']" placeholder="请输入收货人邮编"></a-input>
            </a-form-item>
          </a-col>
          <!-- <a-col :xs="24" :sm="12">
            <a-form-item label="省市区" :labelCol="labelCol" :wrapperCol="wrapperCol">
             <j-area-linkage type="select" v-decorator="['receiverRegion']" placeholder="请输入省市区" @change="change" />
              <JAreaLinkage  @change="areaChange" :value="city"></JAreaLinkage>
            </a-form-item>
          </a-col> -->
          <!--<a-col :xs="24" :sm="12">
            <a-form-item label="省份/直辖市" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['receiverProvince']" placeholder="请输入省份/直辖市"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="城市" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['receiverCity']" placeholder="请输入城市"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="区" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['receiverRegion']" placeholder="请输入区"></a-input>
            </a-form-item>
          </a-col>-->
          <a-col :xs="24" :sm="12">
            <a-form-item label="详细地址" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['receiverDetailAddress']" placeholder="请输入详细地址"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="订单备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['note']" placeholder="请输入订单备注"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="推荐人" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['refereesCustomerId']" placeholder="请输入推荐人"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="推荐积分" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['refereesIntegral', validatorRules.refereesIntegral]" placeholder="请输入推荐积分" style="width: 100%"/>
            </a-form-item>
          </a-col>
<!--          -->
          <a-col :xs="24" :sm="12">
            <a-form-item label="提货人姓名" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['proposerName']" placeholder="请输入提货人姓名"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="提货人手机号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['proposerPhone']" placeholder="请输入提货人手机号"></a-input>
            </a-form-item>
          </a-col>

        </a-row>
      </a-form>

      <!-- 子表单区域 -->
      <a-tabs v-model="activeKey" @change="handleChangeTabs">
        <a-tab-pane tab="订单列表" :key="refKeys[0]" :forceRender="true">
          <j-editable-table
            :ref="refKeys[0]"
            :loading="orderGoodsTable.loading"
            :columns="orderGoodsTable.columns"
            :dataSource="orderGoodsTable.dataSource"
            :maxHeight="300"
            :rowNumber="true"
            :rowSelection="true"
            :actionButton="false"/>
        </a-tab-pane>

      </a-tabs>
      </j-form-container>
    </a-spin>
  </j-modal>
</template>

<script>

  import pick from 'lodash.pick'
  import JFormContainer from '@/components/jeecg/JFormContainer'
  import { FormTypes,getRefPromise } from '@/utils/JEditableTableUtil'
  import { JEditableTableMixin } from '@/mixins/JEditableTableMixin'
  import { validateDuplicateValue } from '@/utils/util'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"
  import JAreaLinkage from '@/components/jeecg/JAreaLinkage'
  import Area from '@/components/_util/Area'
  export default {
    name: 'OrderModal',
    mixins: [JEditableTableMixin],
    components: {
      JDictSelectTag,
      JAreaLinkage
    },
    props: {
      //流程表单data
      formData: {
        type: Object,
        default: ()=>{},
        required: false
      },
      //表单模式：true流程表单 false普通表单
      formBpm: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data() {

      return {
        pcu:'',
        //省，市，区
        sheng:'',
        shi:'',
        city:'',
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        labelCol2: {
          xs: { span: 24 },
          sm: { span: 3 },
        },
        wrapperCol2: {
          xs: { span: 24 },
          sm: { span: 20 },
        },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
          customerId: {
            rules: [
              { required: true, message: '请输入客户编号!'},
            ]
          },
          totalAmount: {
            rules: [
              { required: true, message: '请输入订单总金额!'},
            ]
          },
          payAmount: {
            rules: [
              { required: true, message: '请输入应付金额（实际支付金额）!'},
            ]
          },
          freightAmount: {
            rules: [
              { required: true, message: '请输入运费金额!'},
            ]
          },
          couponAmount: {
            rules: [
              { required: true, message: '请输入优惠券抵扣金额!'},
            ]
          },
          payType: {
            rules: [
              { required: true, message: '请输入支付方式[0:未支付,1:微信,2:支付宝]!'},
            ]
          },
          sourceType: {
            rules: [
              { required: true, message: '请输入订单来源[1:微信小程序,2:app,3:pc]!'},
            ]
          },
          orderType: {
            rules: [
              { required: true, message: '请输入订单类型[0:正常订单,1:团购订单]!'},
            ]
          },
          integral: {
            rules: [
              { required: true, message: '请输入可以获得的积分!'},
            ]
          },
          refereesIntegral: {
            rules: [
              { required: true, message: '请输入推荐积分!'},
            ]
          },
        },
        refKeys: ['orderGoods', ],
        tableKeys:['orderGoods', ],
        activeKey: 'orderGoods',
        // biz_order_goods
        orderGoodsTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '订单编号',
              key: 'orderId',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '商品名称',
              key: 'goodsName',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '购买数量',
              key: 'num',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '商品单价',
              key: 'price',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '商品金额',
              key: 'totalPrice',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '单位',
              key: 'unit',
              type: FormTypes.input,
              defaultValue: '',
            },
            /*{
              title: '创建人',
              key: 'createBy',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '创建时间',
              key: 'createTime',
              type: FormTypes.date,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '更新人',
              key: 'updateBy',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '更新时间',
              key: 'updateTime',
              type: FormTypes.date,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '是否删除',
              key: 'deleted',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },*/
          ]
        },
        url: {
          add: "/order/order/add",
          edit: "/order/order/edit",
          orderGoods: {
            list: '/order/order/queryOrderGoodsByMainId'
          },
        }
      }
    },
    computed: {
      formDisabled(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return false
          }
          return true
        }
        return this.disabled
      },
      showFlowSubmitButton(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return true
          }
        }
        return false
      }
    },
    created () {
      //如果是流程中表单，则需要加载流程表单data
      // this.showFlowData();
      this.pcaData = new Area()
    },
    methods: {
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        let fieldval = pick(this.model,'couponId','customerId','shopName','totalAmount','payAmount','freightAmount','couponAmount','payType','payNo','refundNo','sourceType','status','orderType','deliveryCompany','deliveryCode','autoConfirmDay','integral','receiverName','receiverPhone','receiverPostCode','receiverDetailAddress','note','refereesCustomerId','refereesIntegral','proposerName','proposerPhone')
        this.$nextTick(() => {
          this.city = this.model.receiverRegion
          this.form.setFieldsValue(fieldval)
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.orderGoods.list, params, this.orderGoodsTable)
        }
      },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)

        return {
          ...main, // 展开
          orderGoodsList: allValues.tablesValue[0].values,
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },
     popupCallback(row){
       this.form.setFieldsValue(pick(row,'couponId','customerId','shopId','totalAmount','payAmount','freightAmount','couponAmount','payType','payNo','refundNo','sourceType','status','orderType','deliveryCompany','deliveryCode','autoConfirmDay','integral','receiverName','receiverPhone','receiverPostCode','receiverProvince','receiverCity','receiverRegion','receiverDetailAddress','note','refereesCustomerId','refereesIntegral','createBy','createTime','proposerName','proposerPhone'))
     },
      //省市联动
      areaChange(e){
        console.log(e)
        this.city = e
      }
    }
  }
</script>

<style scoped>
</style>